﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DonationTracker
{
    public partial class Charities1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (User.Identity.IsAuthenticated)
            {
                if (User.IsInRole("Administrator"))
                {
                    grd1.AutoGenerateEditButton = true;
                    grd1.AutoGenerateDeleteButton = true;
                }
            }
            if (!IsPostBack) grd1.ShowFooter = false;
        }

        protected void btnAddCharity_Click(object sender, EventArgs e)
        {
            grd1.ShowFooter = true;
        }

        protected void grd1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Insert" && Page.IsValid)
            {
                //Locate the controls in the gridview's footer
                TextBox txtNewCharityName =
                    (TextBox)grd1.FooterRow.FindControl("txtNewCharityName");
                TextBox txtNewDescription =
                    (TextBox)grd1.FooterRow.FindControl("txtNewDescription");

                //Trim the whitespace from left and right ends of input
                string name, desc;
                name = txtNewCharityName.Text.Trim();
                desc = txtNewDescription.Text.Trim();
                //If description is an empty string, set desc = null
                if (String.IsNullOrWhiteSpace(desc)) desc = null;

                //Set the value of insert parameters to the new/edited values
                donationTracker_dataSource.InsertParameters["name"] =
                    new Parameter("name", System.Data.DbType.String, name);
                donationTracker_dataSource.InsertParameters["description"] =
                    new Parameter("description", System.Data.DbType.String, desc);

                //Insert new record.
                donationTracker_dataSource.Insert();
                grd1.ShowFooter = false;
            }
        }

        protected void grd1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            TextBox txtEditCharityName =
                (TextBox)grd1.Rows[e.RowIndex].FindControl("txtEditCharityName");
            TextBox txtEditDescription =
                (TextBox)grd1.Rows[e.RowIndex].FindControl("txtEditDescription");

            string n, d;
            n = txtEditCharityName.Text.Trim();
            d = txtEditDescription.Text.Trim();
            if (String.IsNullOrWhiteSpace(d)) d = null;

            donationTracker_dataSource.UpdateParameters["name"] =
                new Parameter("name", System.Data.DbType.String, n);
            donationTracker_dataSource.UpdateParameters["description"] =
                new Parameter("description", System.Data.DbType.String, d);
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            grd1.ShowFooter = false;
        }
    }
}